#!/usr/bin/perl
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 2002 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 

#####################################################################
# Subroutines:                                                      #
#                                                                   #
#                                                                   #
#     usage run_cmd getArgs remove_files_dir                        #
#                                                                   #
#                                                                   #
#                                                                   #
#####################################################################

use strict;
BEGIN { $::csmpm = $ENV{'CSM_PM'} ? $ENV{'CSM_PM'} : '/opt/csm/pm'; }
use lib $::csmpm;
use File::Path;	
# Provides mkpath();
use FindBin qw($Bin);
use lib "$Bin";

use Getopt::Std;
use NodeUtils;
use InstallDefs;
use InstallUtils;

$ENV{'CT_MANAGEMENT_SCOPE'} = 1;# set local scope because we only want classes on the mgmt svr

$::MSGCAT = 'csmInstall.cat';
$::MSGMAPPATH = $ENV{'CSM_MSGMAPS'} ? $ENV{'CSM_MSGMAPS'} : '/opt/csm/msgmaps';
$::MSGSET = 'csminstall';

@::My_NodeList=();

# gather the variables
$::ATTRS{'InstallCSMVersion'}=InstallUtils->get_CSMVersion("csm\.core");
$::ATTRS{'InstallOSName'}=InstallUtils->get_OSName;
$::ATTRS{'InstallDistributionName'}=InstallUtils->get_DistributionName;
$::ATTRS{'InstallDistributionVersion'}=InstallUtils->get_DistributionVersion;
$::ATTRS{'InstallPkgArchitecture'}=InstallUtils->get_PkgArchitecture;

$::osTypeVersion=$::ATTRS{'InstallOSName'}.$::ATTRS{'InstallDistributionName'}.$::ATTRS{'InstallDistributionVersion'};

@::csm_logfilelist=@{$::logfiles{'MgmtSvr'}{$::ATTRS{'InstallOSName'} . $::ATTRS{'InstallDistributionName'} . $::ATTRS{'InstallDistributionVersion'}}};
@::csm_uninstall_packages =(@{$::csm_unpackages{'MgmtSvr'}{$::osTypeVersion}{$::ATTRS{'InstallCSMVersion'}}});
@::csm_uninstall_packages = reverse @::csm_uninstall_packages;
@::rsct_uninstall_packages =(@{$::rsct_unpackages{'MgmtSvr'}{$::osTypeVersion}{$::ATTRS{'InstallCSMVersion'}}});
@::rsct_uninstall_packages = reverse @::rsct_uninstall_packages;
@::csmUninstallList_Directories=(@{$::csmUnistallList_dir{'MgmtSvr'}{$::osTypeVersion}{$::ATTRS{'InstallCSMVersion'}}});
@::csmUninstallList_Packages=(@{$::csmUnistallList_pack{'MgmtSvr'}{$::osTypeVersion}{$::ATTRS{'InstallCSMVersion'}}});

sub usage
{

NodeUtils->message('I', 'IMsguninstallmsUsage');
exit 1;
}

sub getArgs
{
   if (! getopts('fuvVh') ) { &usage; } 

   if ($::opt_h) { &usage }
   if ($::opt_v || $::opt_V)
   {
     if ($::opt_v) { $::VERBOSE = $::opt_v; }
     if ($::opt_V) { $::VERBOSE = $::opt_V; }
   }

   if (  (!($::opt_f)) && ($::opt_u) )
   {
      NodeUtils->message('P', 'IMsgUNINSTALLMS_VERIFY_U');
      my $input = getc;
      if (($input !~ /^y/) and ($input !~ /^Y/))
      {
        NodeUtils->message('I', 'IMsgUNINSTALLMS_NOT_UNINSTALLING');
        exit 0;
      }
   }
   if (  (!($::opt_f)) && (!($::opt_u)) )
   {
      NodeUtils->message('P', 'IMsgUNINSTALLMS_VERIFY');
      my $input = getc;
      if (($input !~ /^y/) and ($input !~ /^Y/))
      {
        NodeUtils->message('I', 'IMsgUNINSTALLMS_NOT_UNINSTALLING');
        exit 0;
      }
   }

}

sub remove_files_dir
{
   my ($file_name, undef)=@_;
   NodeUtils->runcmd("$::RM -rf $file_name",-1);
   if ($::RUNCMD_RC !=0)
   {
     NodeUtils->message('W','EMsgCannotDelete',$file_name);
   }
}

###########################################################
#
#
#   Main
#
#
###########################################################

# parse the arguments

&getArgs;

my $runoption = "-1";
if($::VERBOSE){
	$runoption = "0";
}
# Remove the members of a Node Group 
# NodeUtils->runrmccmd('chrsrc-api', '-i',  qq(-s IBM.NodeGroup::"MemberList!=''"::"MemberList::"{}""));
# don't use runrmccmd because it will exit if there is a problem
NodeUtils->runcmd("/usr/bin/chrsrc-api -i -s IBM.NodeGroup::\"MemberList!=''\"::\"MemberList::\"{}\"\"", $runoption, 0);
if ($::RUNCMD_RC!=0)
{
  NodeUtils->message('W', 'EMsgCannotRemoveNodegrpMembers');
}

# Remove the  Node groups from the system
# NodeUtils->runrmccmd('rmrsrc-api', '-i -s', qq(IBM.NodeGroup::"Name!=''"));
# don't use runrmccmd because it will exit if there is a problem
NodeUtils->runcmd("/usr/bin/rmrsrc-api -i -s IBM.NodeGroup::\"Name!=''\"", $runoption, 0);
if ($::RUNCMD_RC!=0)
{
  NodeUtils->message('W', 'EMsgCannotRemoveNodegrp');
}

# get the node list
my $myresult=NodeUtils->listNodeAttrs([],"Hostname");
foreach my $my_node (@$myresult)
{
   push (@::My_NodeList,$my_node)
}

# Running the rmnode command
my $nodecnt=0;
$nodecnt=@::My_NodeList;
if ($nodecnt !=0)
{
   my $rm_cmd="$::RMNODE";
   if ($::VERBOSE)
   {
      $rm_cmd .= " -v";
   }
   if ($::opt_u)
   {
     $rm_cmd .= " -u" ;
   } 
   my $mycmd= "$rm_cmd @::My_NodeList";
   $myresult=NodeUtils->runcmd("$mycmd",$runoption);
   if ($::RUNCMD_RC !=0)
   {
      NodeUtils->message('W','EMsgCANT_RUN_CMD',$mycmd);
   }
}

# Remove the CSM packages
my ($my_package, $my_logfile,$my_cmd, $my_result); 

# remove the pre-defined-condresposes
# this is done in csmserveruncfg

if ($::ATTRS{'InstallOSName'} eq "AIX")
{
   # remove the CSM packages
   foreach $my_package (@::csm_uninstall_packages)
   {
     $my_cmd="$::INSTALLPCMD -u $my_package";
     $my_result=NodeUtils->runcmd("$my_cmd",$runoption);
     if ($::RUNCMD_RC !=0)
     {
       NodeUtils->message('W','EMsgCANT_RUN_CMD',$my_cmd);
     }
   }

   # remove the RSCT packages
   foreach $my_package (@::rsct_uninstall_packages)
   {
     $my_cmd="$::INSTALLPCMD -u $my_package";
     $my_result=NodeUtils->runcmd("$my_cmd",$runoption);
     if ($::RUNCMD_RC !=0)
     {
       NodeUtils->message('W','EMsgCANT_RUN_CMD',$my_cmd);
     }
   }
}
elsif ($::ATTRS{'InstallOSName'} eq "Linux")
{
   # remove the CSM packages

## Below is the temp. fix for libjvm problem
#   my $my_temp_str="";
#   $my_temp_str=join(" ",@::csm_uninstall_packages);
#   $my_cmd="$::RPMCMD -e $my_temp_str";
#   $my_result=NodeUtils->runcmd("$my_cmd",$runoption);
#   if ($::RUNCMD_RC !=0)
#   {
#      NodeUtils->message('W','EMsgCANT_RUN_CMD',$my_cmd);
#   }


   foreach $my_package (@::csm_uninstall_packages)
   {
     $my_cmd="$::RPMCMD -q $my_package 2>&1 > /dev/null";
     $my_result=NodeUtils->runcmd("$my_cmd",$runoption);
     if ($::RUNCMD_RC == 0)	# If it is installed, uninstall it
     {
	 $my_cmd="$::RPMCMD -e $my_package";
	 $my_result=NodeUtils->runcmd("$my_cmd",$runoption);
	 if ($::RUNCMD_RC !=0)
	 {
	   NodeUtils->message('W','EMsgCANT_RUN_CMD',$my_cmd);
	 }
      }
   }

   # remove the RSCT packages
   foreach $my_package (@::rsct_uninstall_packages)
   {
     $my_cmd="$::RPMCMD -q $my_package 2>&1 > /dev/null";
     $my_result=NodeUtils->runcmd("$my_cmd",$runoption);
     if ($::RUNCMD_RC == 0)	# If it is installed, uninstall it
     {
	 $my_cmd="$::RPMCMD -e $my_package";
	 $my_result=NodeUtils->runcmd("$my_cmd",$runoption);
	 if ($::RUNCMD_RC !=0)
	 {
	   NodeUtils->message('W','EMsgCANT_RUN_CMD',$my_cmd);
	 }
     }
   }
}

# remove the log files package
foreach my $my_logfile (@::csm_logfilelist)
{
  &remove_files_dir($my_logfile);
}

#Print The files, Directories and packages that were not uninstalled as there were not been installed by installms

NodeUtils->message('I','IMsgDidnotRemoveDir',join(', ',@::csmUninstallList_Directories));
NodeUtils->message('I','IMsgDidnotRemovePkgs',join(', ',@::csmUninstallList_Packages));
NodeUtils->message('I','IMsgDidnotUninstallOpenSrcPkgs');
NodeUtils->message('I','IMsgDidnotUnconfigureRemoteShellServer');

exit 0;
